//
//  HKTabBarController.m
//  BaiSiBuDeJie
//
//  Created by 胡锦涛 on 2018/11/30.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKTabBarController.h"
#import "HKEssenceViewController.h"
#import "HKMeViewController.h"
#import "HKFriendTrendViewController.h"
#import "HKPublishViewController.h"
#import "HKNewViewController.h"
#import "HKTabBar.h"
#import "HKNavigationController.h"
@interface HKTabBarController ()

@end

@implementation HKTabBarController
/*
 问题:
 1.选中的图片被渲染 :iOS 7以后tabBar图片都会被渲染，修改图片的renderModel 为original
 2.选中按钮的标题颜色:黑色 标题字体大-> 对应子控制器的tabBarItem
 3.发布按钮显示不出来：分析：发布按钮图片太大，导致显示不出来
 图片太大，系统帮你渲染==> 能显示 ==> 位置不对 ==>高亮状态达不到
 解决：不能修改图片尺寸 效果：让发布图片居中
 2.如何解决:系统的TabBar上按钮状态只有选中,没有高亮状态 => 中间发布按钮 不能用系统tabBarButton => 发布按钮 不是 tabBarController子控制器
 1.自定义tabBar
 */

//只会调用一次
+(void)load {
    /*
     appearance:
     1.只要遵守了UIAppearance协议，还要实现这个方法
     2.哪些属性可以通过appearance 设置，只有被UI_APPEARANCE_SELECTOR宏修饰的属性，才能设置
     3.[UITabBarItem appearance] 默认设置全局(先remove 然后设置appearance 最后再addSubView)
     [self.view removeFromSuperView];
     
     UISwitch *switchView = [UISwitch appearance];
     switchView.onTinColor = [UIColor redColor];
     [[UIApplication sharedApplication].keyWindow addSubView:self.view];
     */
    
    //获取整个程序下的TabBarItem
//    UITabBarItem * tabBarItem = [UITabBarItem appearance];
    //开发中建议使用这种 只在某个类设置统一属性
    //iOS 9.0之前
    //UITabBarItem * tabBarItem = [UITabBarItem appearanceWhenContainedIn:self, nil];
    //iOS 9.0之后
    UITabBarItem * tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[HKTabBarController.self]];
    //设置按钮选中标题的颜色:富文本 、颜色、字体、阴影、空心、图文混排
    //创建一个描述文本属性的字典
    NSMutableDictionary * normalAttrs = [NSMutableDictionary dictionary];
    //设置字体大小(正常状态才有效)
    normalAttrs[NSFontAttributeName] = kFont(13);
    [tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary];
    //设置字体颜色
    selectedAttrs[NSForegroundColorAttributeName] = kBlackColor;
    //设置字体大小(正常状态才有效)
    selectedAttrs[NSFontAttributeName] = kFont(13);
    [tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

//注意：可能会调用多次
+ (void)initialize {
    if (self == [HKTabBarController class]) {
        
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //1.添加子控制器
    [self setupAllChildViewController];
    //2.设置tabBar上所有的内容
    [self setupAllTitleButton];
    // 3.自定义tabBar
    [self setupTabBar];
    // tabBar上按钮并不是在viewDidLoad添加的
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%@",self.tabBar.subviews);
}
#pragma mark - 自定义tabBar
- (void)setupTabBar
{
    //思路：调整系统tabBar上按钮位置，平均分成5等分，在把加号按钮显示在中间就好了
    //调整系统自带控件的子控件的位置 ==> 自定义tabBar
    HKTabBar *tabBar = [[HKTabBar alloc] init];
    //self.tabBar = tabBar;
    [self setValue:tabBar forKey:@"tabBar"];
}

/*
 Changing the delegate of a tab bar 【managed by a tab bar controller】 is not allowed.
 被UITabBarController所管理的UITabBar的delegate是不允许修改的
 */

- (void)setupAllChildViewController {
    //2.1.添加子控制器
    //精华
    HKEssenceViewController * essenceVC = [HKEssenceViewController new];
    HKNavigationController * essenceNav = [[HKNavigationController alloc] initWithRootViewController:essenceVC];
    //tabBarVc 会把第0个子控制器的View添加进去
    [self addChildViewController:essenceNav];
    //新帖
    HKNewViewController * newVC = [HKNewViewController new];
    HKNavigationController * newNav = [[HKNavigationController alloc] initWithRootViewController:newVC];
    [self addChildViewController:newNav];

    //关注
    HKFriendTrendViewController * friendTrendVC = [HKFriendTrendViewController new];
    HKNavigationController * friendTrendNav = [[HKNavigationController alloc] initWithRootViewController:friendTrendVC];
    [self addChildViewController:friendTrendNav];
    //我
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HKMeViewController" bundle:nil];
    //加载箭头指向的VC
    HKMeViewController * meVC = [storyBoard instantiateInitialViewController ];
    //HKMeViewController * meVC = [HKMeViewController new];
    HKNavigationController * meNav = [[HKNavigationController alloc] initWithRootViewController:meVC];
    [self addChildViewController:meNav];
}

- (void)setupAllTitleButton {
    //2.2.设置tabBar上按钮的内容
    HKNavigationController * essenceNav = self.childViewControllers[0];
    essenceNav.tabBarItem.title = @"精华";
    essenceNav.tabBarItem.image = [UIImage noRenderingImageName:@"tabBar_essence_icon"];
    essenceNav.tabBarItem.selectedImage = [UIImage noRenderingImageName:@"tabBar_essence_click_icon"];
    
    HKNavigationController * newNav = self.childViewControllers[1];
    newNav.tabBarItem.title = @"新帖";
    newNav.tabBarItem.image = [UIImage noRenderingImageName:@"tabBar_new_icon"];
    newNav.tabBarItem.selectedImage = [UIImage noRenderingImageName:@"tabBar_new_click_icon"];
    
    HKNavigationController * friendTrendNav = self.childViewControllers[2];
    friendTrendNav.tabBarItem.title = @"关注";
    friendTrendNav.tabBarItem.image = [UIImage noRenderingImageName:@"tabBar_friendTrends_icon"];
    friendTrendNav.tabBarItem.selectedImage = [UIImage noRenderingImageName:@"tabBar_friendTrends_click_icon"];
    
    HKNavigationController * meNav = self.childViewControllers[3];
    meNav.tabBarItem.title = @"我";
    meNav.tabBarItem.image = [UIImage noRenderingImageName:@"tabBar_me_icon"];
    meNav.tabBarItem.selectedImage = [UIImage noRenderingImageName:@"tabBar_me_click_icon"];
    
    //设置图片的位置
    //publishVC.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -10, 0); //会使图片压扁
}

@end
